import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QSpacerItem, QSizePolicy
from PyQt5.QtCore import Qt, QEvent


from main_ui import Ui_MainWindow  # 导入 uiDemo9.py 中的 Ui_MainWindow 界面类
from mylabel import DraggableLabel


class MyMainWindow(QMainWindow, Ui_MainWindow):  # 继承 QMainWindow 类和 Ui_MainWindow 界面类
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)  # 初始化父类
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setupUi(self)  # 继承 Ui_MainWindow 界面类
        self.R = None

        # 屏幕最大化

        self.init(3)
        self.setConnect()
        self.showMaximized()
        self.setAcceptDrops(True)


    def init(self, n):
        self.spinBox.setValue(n)
        self.index = [list(range(n)), [], []]
        self.upDate()


    def upDate(self):
        n = self.spinBox.value()
        self.R = []
        minw = 100
        maxw = 200
        d = maxw-minw
        if n == 1:
            w = [maxw]
        else:
            dw = d//(n-1)
            w = [minw+i*dw for i in range(n)]
        self.R = []
        for i, wi in enumerate(w):
            d = DraggableLabel(i, wi)
            d.move_.connect(self.move_)
            self.R.append(d)

        self.showR()

        # self.showMaximized()


    # def update(self) -> None:
    #     self.showMaximized()

    def setConnect(self):
        self.pushButton.clicked.connect(lambda :self.init(3))
        self.pushButton_2.clicked.connect(self.close)
        self.spinBox.valueChanged.connect(self.init)


    def remove(self, layout):
        # 假设我们稍后想要清空布局
        for i in reversed(range(layout.count())):
            item = layout.itemAt(i)
            if item.widget():
                # 如果项包含一个控件，则删除该控件
                item.widget().deleteLater()
                layout.removeWidget(item.widget())
                # item.widget().deleteLater()
            elif item.spacerItem():
                # 如果项是一个SpacerItem，则移除它
                layout.removeItem(item)

    def showR(self):
        # 清空布局中的所有元素
        self.remove(self.verticalLayout)
        self.remove(self.verticalLayout_2)
        self.remove(self.verticalLayout_3)
        for i in self.index:
            if i:
                self.R[i[0]].dable = True

        spacerItem1 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem1)
        for i in self.index[0]:
            self.verticalLayout.addWidget(self.R[i], alignment=Qt.AlignCenter)
        self.d1 = DraggableLabel(-1, 250, 10, '#f00', '#aaa')
        self.verticalLayout.addWidget(self.d1, alignment=Qt.AlignCenter)
        spacerItem2 = QSpacerItem(400, 300, QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.verticalLayout.addItem(spacerItem2)

        spacerItem3 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem3)
        for i in self.index[1]:
            self.verticalLayout_2.addWidget(self.R[i], alignment=Qt.AlignCenter)
        self.d2 = DraggableLabel(-1, 250, 10, '#f00', '#aaa')
        self.verticalLayout_2.addWidget(self.d2, alignment=Qt.AlignCenter)
        spacerItem4 = QSpacerItem(400, 300, QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.verticalLayout_2.addItem(spacerItem4)

        spacerItem5 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.verticalLayout_3.addItem(spacerItem5)
        for i in self.index[2]:
            self.verticalLayout_3.addWidget(self.R[i], alignment=Qt.AlignCenter)
        self.d3 = DraggableLabel(-1, 250, 10, '#f00', '#aaa')
        self.verticalLayout_3.addWidget(self.d3, alignment=Qt.AlignCenter)
        spacerItem6 = QSpacerItem(400, 300, QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.verticalLayout_3.addItem(spacerItem6)

        self.d1_ = DraggableLabel(-1, 10, 250, '#f00', '#aaa', parent=self)
        self.d2_ = DraggableLabel(-1, 10, 250, '#f00', '#aaa', parent=self)
        self.d3_ = DraggableLabel(-1, 10, 250, '#f00', '#aaa', parent=self)


    def dragEnterEvent(self, event):
        # 当拖拽进入主窗口时，如果携带了图片数据，则接受拖拽
        event.accept()

    def move_(self, i, x, y):
        d1x, d1y = self.d1.pos().x(), self.d1.pos().y()
        d2x, d2y = self.d2.pos().x(), self.d2.pos().y()
        d3x, d3y = self.d3.pos().x(), self.d3.pos().y()
        if x>d1x and x<d1x+250:
            if i not in self.index[0]:
                if self.index[0] == [] or self.index[0] and self.index[0][0]>i:
                    self.index[0].insert(0, i)
                    if i in self.index[1]:
                        self.index[1].remove(i)
                    elif i in self.index[2]:
                        self.index[2].remove(i)
        elif x>d2x and x<d2x+250:
            if i not in self.index[1]:
                if self.index[1] == [] or (self.index[1] and self.index[1][0]>i):
                    self.index[1].insert(0, i)
                    if i in self.index[0]:
                        self.index[0].remove(i)
                    elif i in self.index[2]:
                        self.index[2].remove(i)
        elif x>d3x and x<d3x+250:
            if i not in self.index[2]:
                if self.index[2] == [] or self.index[2] and self.index[2][0]>i:
                    self.index[2].insert(0, i)
                    if i in self.index[0]:
                        self.index[0].remove(i)
                    elif i in self.index[1]:
                        self.index[1].remove(i)
        self.upDate()

    def changeEvent(self, event):
        if event.type() == 99:
            # 当窗口最大化时，获取控件的frameGeometry
            self.d1_.move(self.d1.x()+self.d1.width()/2-self.d1_.width()/2, self.d1.y()-self.d1_.height())
            self.d1_.lower()
            self.d2_.move(self.d2.x() + self.d2.width() / 2 - self.d2_.width() / 2, self.d2.y() - self.d2_.height())
            self.d2_.lower()
            self.d3_.move(self.d3.x() + self.d3.width() / 2 - self.d3_.width() / 2, self.d3.y() - self.d3_.height())
            self.d3_.lower()


        super().changeEvent(event)






if __name__ == '__main__':  # youcans, XUPT 2021
    app = QApplication(sys.argv)  # 在 QApplication 方法中使用，创建应用程序对象
    myWin = MyMainWindow()  # 实例化 MyMainWindow 类，创建主窗口
    myWin.show()  # 在桌面显示控件 myWin

    sys.exit(app.exec_())  # 结束进程，退出程序



